package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.FunctionDAO;
import cn.edu.scau.cmi.hombio.dao.RoleAuthorityDAO;
import cn.edu.scau.cmi.hombio.dao.RoleDAO;

import cn.edu.scau.cmi.hombio.domain.Function;
import cn.edu.scau.cmi.hombio.domain.Role;
import cn.edu.scau.cmi.hombio.domain.RoleAuthority;

import cn.edu.scau.cmi.hombio.service.RoleAuthorityService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for RoleAuthority entities
 * 
 */

@Controller("RoleAuthorityRestController")

public class RoleAuthorityRestController {

	/**
	 * DAO injected by Spring that manages Function entities
	 * 
	 */
	@Autowired
	private FunctionDAO functionDAO;

	/**
	 * DAO injected by Spring that manages RoleAuthority entities
	 * 
	 */
	@Autowired
	private RoleAuthorityDAO roleAuthorityDAO;

	/**
	 * DAO injected by Spring that manages Role entities
	 * 
	 */
	@Autowired
	private RoleDAO roleDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for RoleAuthority entities
	 * 
	 */
	@Autowired
	private RoleAuthorityService roleAuthorityService;

	/**
	 * Save an existing Role entity
	 * 
	 */
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/role", method = RequestMethod.PUT)
	@ResponseBody
	public Role saveRoleAuthorityRole(@PathVariable Integer roleauthority_id, @RequestBody Role role) {
		roleAuthorityService.saveRoleAuthorityRole(roleauthority_id, role);
		return roleDAO.findRoleByPrimaryKey(role.getId());
	}

	/**
	* Create a new RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority", method = RequestMethod.POST)
	@ResponseBody
	public RoleAuthority newRoleAuthority(@RequestBody RoleAuthority roleauthority) {
		roleAuthorityService.saveRoleAuthority(roleauthority);
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority.getId());
	}

	/**
	* Get Role entity by RoleAuthority
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/role", method = RequestMethod.GET)
	@ResponseBody
	public Role getRoleAuthorityRole(@PathVariable Integer roleauthority_id) {
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority_id).getRole();
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Delete an existing Function entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/function/{function_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRoleAuthorityFunction(@PathVariable Integer roleauthority_id, @PathVariable Integer related_function_id) {
		roleAuthorityService.deleteRoleAuthorityFunction(roleauthority_id, related_function_id);
	}

	/**
	* Show all RoleAuthority entities
	* 
	*/
	@RequestMapping(value = "/RoleAuthority", method = RequestMethod.GET)
	@ResponseBody
	public List<RoleAuthority> listRoleAuthoritys() {
		return new java.util.ArrayList<RoleAuthority>(roleAuthorityService.loadRoleAuthoritys());
	}

	/**
	* Get Function entity by RoleAuthority
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/function", method = RequestMethod.GET)
	@ResponseBody
	public Function getRoleAuthorityFunction(@PathVariable Integer roleauthority_id) {
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority_id).getFunction();
	}

	/**
	* Create a new Function entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/function", method = RequestMethod.POST)
	@ResponseBody
	public Function newRoleAuthorityFunction(@PathVariable Integer roleauthority_id, @RequestBody Function function) {
		roleAuthorityService.saveRoleAuthorityFunction(roleauthority_id, function);
		return functionDAO.findFunctionByPrimaryKey(function.getId());
	}

	/**
	* Save an existing RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority", method = RequestMethod.PUT)
	@ResponseBody
	public RoleAuthority saveRoleAuthority(@RequestBody RoleAuthority roleauthority) {
		roleAuthorityService.saveRoleAuthority(roleauthority);
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority.getId());
	}

	/**
	* Create a new Role entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/role", method = RequestMethod.POST)
	@ResponseBody
	public Role newRoleAuthorityRole(@PathVariable Integer roleauthority_id, @RequestBody Role role) {
		roleAuthorityService.saveRoleAuthorityRole(roleauthority_id, role);
		return roleDAO.findRoleByPrimaryKey(role.getId());
	}

	/**
	* Delete an existing RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRoleAuthority(@PathVariable Integer roleauthority_id) {
		RoleAuthority roleauthority = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority_id);
		roleAuthorityService.deleteRoleAuthority(roleauthority);
	}

	/**
	* Select an existing RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}", method = RequestMethod.GET)
	@ResponseBody
	public RoleAuthority loadRoleAuthority(@PathVariable Integer roleauthority_id) {
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority_id);
	}

	/**
	* Save an existing Function entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/function", method = RequestMethod.PUT)
	@ResponseBody
	public Function saveRoleAuthorityFunction(@PathVariable Integer roleauthority_id, @RequestBody Function function) {
		roleAuthorityService.saveRoleAuthorityFunction(roleauthority_id, function);
		return functionDAO.findFunctionByPrimaryKey(function.getId());
	}

	/**
	* View an existing Function entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/function/{function_id}", method = RequestMethod.GET)
	@ResponseBody
	public Function loadRoleAuthorityFunction(@PathVariable Integer roleauthority_id, @PathVariable Integer related_function_id) {
		Function function = functionDAO.findFunctionByPrimaryKey(related_function_id, -1, -1);

		return function;
	}

	/**
	* Delete an existing Role entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/role/{role_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRoleAuthorityRole(@PathVariable Integer roleauthority_id, @PathVariable Integer related_role_id) {
		roleAuthorityService.deleteRoleAuthorityRole(roleauthority_id, related_role_id);
	}

	/**
	* View an existing Role entity
	* 
	*/
	@RequestMapping(value = "/RoleAuthority/{roleauthority_id}/role/{role_id}", method = RequestMethod.GET)
	@ResponseBody
	public Role loadRoleAuthorityRole(@PathVariable Integer roleauthority_id, @PathVariable Integer related_role_id) {
		Role role = roleDAO.findRoleByPrimaryKey(related_role_id, -1, -1);

		return role;
	}
}